/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IngresarUsuario.java
 *
 * Created on 24/09/2011, 10:17:09 AM
 */

package GUI;

import Control.UsuarioJpaController;
import Control.exceptions.PreexistingEntityException;
import Entidad.Usuario;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Andrés 2
 */
public class IngresarUsuario extends javax.swing.JPanel {

    /** Creates new form IngresarUsuario */
    public IngresarUsuario() {
        initComponents();
        
        for (int i = 1900; i <= 2011; i++) {
            this.añoCB.addItem(i);            
        }
        
        usuarioDao = new UsuarioJpaController();
        
        telefonoTF.addKeyListener(new KeyAdapter(){
            @Override
            public void keyTyped(KeyEvent e)    {
                char caracter = e.getKeyChar();
                if(((caracter < '0') || (caracter > '9')) &&
                (caracter != KeyEvent.VK_BACK_SPACE )){
                    e.consume();
                }
            }
        });
        
        cedulaTF.addKeyListener(new KeyAdapter(){
            @Override
            public void keyTyped(KeyEvent e)    {
                char caracter = e.getKeyChar();
                if(((caracter < '0') || (caracter > '9')) &&
                (caracter != KeyEvent.VK_BACK_SPACE )){
                    e.consume();
                }
            }
        });
    }
    
    private Usuario u ;
    private UsuarioJpaController usuarioDao;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelJL = new javax.swing.JLabel();
        cedulaTF = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cancelarB = new javax.swing.JButton();
        ingresarB = new javax.swing.JButton();
        diaCB = new javax.swing.JComboBox();
        mesCB = new javax.swing.JComboBox();
        añoCB = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        userTF = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jPasswordField2 = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        telefonoTF = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        correoTF = new javax.swing.JTextField();
        statussJL = new javax.swing.JLabel();

        labelJL.setText("INGRESAR USUARIO");

        jLabel4.setText("Nombre");

        jLabel3.setText("Cedula");

        jLabel2.setText("Fecha de nacimiento");

        cancelarB.setText("Cancelar");
        cancelarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarBActionPerformed(evt);
            }
        });

        ingresarB.setText("Ingresar");
        ingresarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ingresarBActionPerformed(evt);
            }
        });

        diaCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dia", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        mesCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mes", "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));

        añoCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Año" }));
        añoCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                añoCBActionPerformed(evt);
            }
        });

        jLabel1.setText("Usuario");

        userTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userTFActionPerformed(evt);
            }
        });

        jLabel6.setText("Contraseña");

        jLabel5.setText("Repetir Contraseña");

        jLabel7.setText("Teléfono");

        telefonoTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                telefonoTFActionPerformed(evt);
            }
        });

        jLabel8.setText("Correo electronico");

        correoTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                correoTFActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                                    .addComponent(cedulaTF, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                                    .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(nombreTF, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(userTF, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(98, 98, 98))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel8))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(correoTF)
                                    .addComponent(telefonoTF, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE))
                                .addGap(96, 96, 96))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(labelJL))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(diaCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mesCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(añoCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(statussJL, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(120, 120, 120)
                        .addComponent(ingresarB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelarB)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cedulaTF, jPasswordField1, jPasswordField2, nombreTF, telefonoTF, userTF});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelJL)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cedulaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(userTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(telefonoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(correoTF, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(diaCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mesCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(añoCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(statussJL, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelarB)
                    .addComponent(ingresarB))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cedulaTF, correoTF, jPasswordField1, jPasswordField2, nombreTF, telefonoTF, userTF});

    }// </editor-fold>//GEN-END:initComponents

private void añoCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_añoCBActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_añoCBActionPerformed

private void ingresarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ingresarBActionPerformed
    u = new Usuario();
    statussJL.setText("");
    
    if(nombreTF.getText().equals("") ||  cedulaTF.getText().equals("")  || userTF.getText().equals("") ||
            jPasswordField1.getText().equals("") || jPasswordField2 .getText().equals("") || añoCB.getSelectedIndex() == 0 ||
            mesCB.getSelectedIndex() == 0 || diaCB.getSelectedIndex() == 0 || telefonoTF.getText().equals("") || correoTF.getText().equals("")){
        
        statussJL.setText("Ningun campo puede estar vacio");
    }else{
        if(!jPasswordField1.getText().equals(jPasswordField2.getText())){
            
            statussJL.setText("Las contraseñas introducidas no coinciden");
        }else{
            if(correoTF.getText().contains("@")){
                statussJL.setText("El correo es incorrecto");
            }else{
             
                u.setNombre(nombreTF.getText());
                u.setCedula( Long.parseLong(cedulaTF.getText()));
                u.setUser(userTF.getText());
                u.setContraseña(jPasswordField1.getText());
                u.setFechaDeNacimiento(new Date(añoCB.getSelectedIndex() - 1,
                        mesCB.getSelectedIndex() - 1, diaCB.getSelectedIndex()));
                u.setTelefono(Integer.parseInt(telefonoTF.getText()));
                u.setCorreoElectronico(correoTF.getText());

                try {
                    usuarioDao.create(u);
                    statussJL.setText("El usuario ha sido creado correctamente");
                } catch (PreexistingEntityException ex) {
                    Logger.getLogger(IngresarUsuario.class.getName()).log(Level.SEVERE, null, ex);
                    statussJL.setText("Ya hay un usuario creado con la cedula digitada");
                } catch (Exception ex) {
                    Logger.getLogger(IngresarUsuario.class.getName()).log(Level.SEVERE, null, ex);
                    statussJL.setText("El usuario no ha podido ser creado");
                }
            }
        }
    }
    
        
}//GEN-LAST:event_ingresarBActionPerformed

private void userTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userTFActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_userTFActionPerformed

private void correoTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_correoTFActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_correoTFActionPerformed

private void telefonoTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_telefonoTFActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_telefonoTFActionPerformed

private void cancelarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBActionPerformed
    nombreTF.setText("");
    cedulaTF.setText("");
    telefonoTF.setText("");
    jPasswordField1.setText("");
    jPasswordField2.setText("");
    correoTF.setText("");
    añoCB.setSelectedIndex(0);
    mesCB.setSelectedIndex(0);
    diaCB.setSelectedIndex(0);
    statussJL.setText("");
}//GEN-LAST:event_cancelarBActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox añoCB;
    private javax.swing.JButton cancelarB;
    private javax.swing.JTextField cedulaTF;
    private javax.swing.JTextField correoTF;
    private javax.swing.JComboBox diaCB;
    private javax.swing.JButton ingresarB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JLabel labelJL;
    private javax.swing.JComboBox mesCB;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JLabel statussJL;
    private javax.swing.JTextField telefonoTF;
    private javax.swing.JTextField userTF;
    // End of variables declaration//GEN-END:variables

}
